package com.silas.dynflightdatatransfer.sharding;

import cn.hutool.core.collection.CollectionUtil;
import com.silas.dynflightdatatransfer.manager.FlightDynamicDataInfoManager;
import com.silas.dynflightdatatransfer.sharding.algorithm.TimeStampSplitByMonth;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 飞行动态数据表加载器
 *
 * @author chentianyan
 */
@Order(value = 1)
@Component
public class FlightDynamicDataInfoTablesLoader implements CommandLineRunner {

    @Value("${spring.shardingsphere.rules.sharding.binding-tables}")
    private String shardingBindTable;

    @Resource
    private FlightDynamicDataInfoManager dynamicDataInfoManager;

    /**
     * 服务启动后，获取数据库中所有飞行动态数据分表，缓存到Set中
     *
     * @param args 启动参数
     */
    @Override
    public void run(String... args) {
        synchronized (TimeStampSplitByMonth.class) {
            List<String> tables =  dynamicDataInfoManager.queryFlightDynamicDataInfoTables(shardingBindTable);
            if (CollectionUtil.isNotEmpty(tables)) {
                TimeStampSplitByMonth.TABLE_NAME_SET.addAll(tables);
            }
        }
    }
}
